/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "StdAfx.h"
#include ".\windowchecker.h"

CWindowChecker* CWindowChecker::theChecker = 0;

int Phantom_RegisterFunction(SSI_FunctionAPI& InAPI, String& CurrClass){
	return CWindowChecker::getInstance()->RegisterFunction(InAPI, CurrClass);
}

CStatus Phantom_CheckFunction(SSI_FunctionAPI& InAPI, String& CurrClass){
	return CWindowChecker::getInstance()->CheckFunction(InAPI, CurrClass);
}

int Phantom_ClearFunctions(){
	return CWindowChecker::getInstance()->ClearFunctions();
}

int Phantom_MapWindow(String& From, String& To){
	return CWindowChecker::getInstance()->MapWindow(From, To);
}

String Phantom_GetMappedWindow(String& From){
	return CWindowChecker::getInstance()->GetMappedWindow(From);
}


CWindowChecker::CWindowChecker(void)
{
	First = 0;
}

CWindowChecker::~CWindowChecker(void)
{
	DelTree(First);
}

int CWindowChecker::RegisterFunction(SSI_FunctionAPI& InAPI, String& CurrClass)
{
	PhantomFunctionNode Curr;
	if(First == SSI_NULL){
		First = NewNode();
		Curr = First;
	}else{
		Curr = First;
		SSI_INT64 NumParams = InAPI.GetNumParams();
		while(Curr){
			if(CompareAPIs(Curr->theAPI, InAPI)){
				Curr->aTree.push(CurrClass);
				return 0;
			}
			if(!Curr->Next) break;
			Curr = Curr->Next;
		}
		Curr->Next = NewNode();
		Curr = Curr->Next;
	}
	Curr->theAPI = InAPI;
	Curr->aTree.push(CurrClass);
	return 1;
}

CStatus CWindowChecker::CheckFunction(SSI_FunctionAPI& InAPI, String& CurrClass)
{
	if(!First) return STAT_ERR;  // Internal Error No functions registered
	
	PhantomFunctionNode Curr = First;
	while(Curr){
		if(CompareAPIs(Curr->theAPI, InAPI)){
			if(Curr->aTree.find(CurrClass) || Curr->aTree.find(String("MainWin"))){
				return STAT_OK;
			}else{
				MAKEERR2(ERR_P00213, InAPI.m_FunctionName.Str(), CurrClass.Str()); // Function not registered to this class
			}
		}
		Curr = Curr->Next;
	}
	MAKEERR2(ERR_P00214, InAPI.m_FunctionName.Str(), CurrClass.Str()); // Function not registered at all
}

PhantomFunctionNode CWindowChecker::NewNode(void)
{
	PhantomFunctionNode aNode = new PhantomFunctionT;
	aNode->Next = SSI_NULL;
	return aNode;
}

void CWindowChecker::DelTree(PhantomFunctionNode aNode)
{
	if(!aNode) return;
	if(aNode->Next) DelTree(aNode->Next);
	delete aNode;
}

int CWindowChecker::ClearFunctions(void)
{
	delete theChecker;
	theChecker = 0;
	return 1;
}

bool CWindowChecker::CompareAPIs(SSI_FunctionAPI& API1, SSI_FunctionAPI& API2)
{
	if(API1.m_FunctionName == API2.m_FunctionName){
		SSI_INT64 NumParams = API1.GetNumParams();
		if(NumParams == API2.GetNumParams()){
			int ctr;
			for(ctr = 0; ctr < NumParams; ctr++){
				if(API1.GetParameterType(ctr) != API2.GetParameterType(ctr)){
					return 0;
				}
			}
			return 1;
		}
	}
	return 0;
}

int CWindowChecker::MapWindow(String& From, String& To)
{
	StringNode* sFrom = 0;
	StringNode* sTo = 0;
	SSI_INT64 index;
	if(sFrom = MapFroms.find(From)){
		index = MapFroms.indexof(sFrom);
		sTo = MapTos[index];
		if(!sTo) return -1;  // Error
		sTo->Set(To);
		return index;
	}else{
		sFrom = MapFroms.push(From);
		sTo = MapTos.push(To);
		index = MapFroms.indexof(sFrom);
		return index;
	}
}

String CWindowChecker::GetMappedWindow(String& From)
{
	StringNode* sFrom = 0;
	StringNode* sTo = 0;
	String Ret;
	SSI_INT64 index;
	if(sFrom = MapFroms.find(From)){
		index = MapFroms.indexof(sFrom);
		sTo = MapTos[index];
		if(!sTo) return From;
		sTo->Get(Ret);
		return Ret;
	}else{
		return From;
	}
}
